package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.ServiceHNUtil;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 权力清单
 */

public class QuanLi_List extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        String rightListUrl = "https://wssp.hainan.gov.cn/wssp3/rightList/getRightListById";
        JSONObject rightListHeader = new JSONObject();
        JSONObject rightListParmas = new JSONObject();
        int rightListPageNum = 1;
        int rightListAllPage = 2;
        while (rightListPageNum <= rightListAllPage) {
            rightListHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            rightListHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/quanli_detail?qdCategory=qlqd&title=%E6%9D%83%E5%8A%9B%E6%B8%85%E5%8D%95");
            rightListParmas.put("regionId", "HZ2881f4424539dd0142453c856b0025");
            rightListParmas.put("pageNum", rightListPageNum);
            rightListParmas.put("pageSize", "10");
            rightListParmas.put("deptid", "");
            rightListParmas.put("powername", "");
            rightListParmas.put("powertype", "");
            String rightListResponse = postFormReq(rightListUrl,rightListHeader,rightListParmas);
            JSONObject rightListJSResponse = JSONObject.parseObject(rightListResponse);
            JSONArray rightList = rightListJSResponse.getJSONObject("data").getJSONArray("resultList");
            for (Object o : rightList){
                JSONObject object = (JSONObject) o;
                JSONObject result = ServiceHNUtil.getListInfo(object);
                System.out.println(result);//测试用代码
                return processResult(
                        null,
                        null,
                        null,
                        null,
                        result,
                        "海南省",
                        "政务公开-权力清单"
                );
            }

            if (rightListPageNum == 1) {
                rightListAllPage = rightListJSResponse.getJSONObject("data").
                        getJSONObject("pageMap").getInteger("pages");
            }
            rightListPageNum++;
        }
        return null;
    }

    public static void main(String[] args) {
        QuanLi_List quanLi_list = new QuanLi_List();
        quanLi_list.getDataDemo();
    }
}
